import { Button, Result } from 'antd'
import React, { useRef } from 'react'
import ProTable, { TableDropdown, ActionType } from '@ant-design/pro-table'
import { columns } from './config'
import { HttpQueryConfig, HttpDeleteConfig, HttpSetConfig } from '@/services'

const ConfigPage: React.FC<{}> = () => {
    const actionRef = useRef<ActionType>()
    const onDelete = async (id: any, row: IT.IConfig) => {
        const { flag, message } = await HttpDeleteConfig({ id })
        if (flag !== 1) {
            throw new Error(message)
        }
        return true
    }
    const onSave = async (id: any, row: IT.IConfig) => {
        const { flag, message } = await HttpSetConfig({
            key: row.key,
            value: row.value,
            note: row.note,
            affiliationType: 0,
        })
        if (flag !== 1) {
            throw new Error(message)
        }
        return true
    }
    return (
        <ProTable<IT.IConfig>
            {...{
                columns,
                actionRef,
                rowKey: 'id',
                bordered: true,
                dateFormatter: 'string',
                headerTitle: '配置管理',
                search: { labelWidth: 'auto' },
                pagination: { pageSize: 50 },
                request: async (params = {}) => await HttpQueryConfig(params),
                editable: {
                    type: 'multiple',
                    onDelete,
                    onSave,
                },
            }}
        />
    )
}

export default ConfigPage
